
/*
________________________________________________________________________________
Define the starts
*/

* Identify main owners
use "$data\99_temp\flownyrtti.dta", clear
bysort syrtun_ownrshp vuosi: egen max_ownrshp = max(ownrshp)
drop if max_ownrshp==. & vuosi>=2006
gen likely_mainowner=0
replace likely_mainowner=1 if yrtti==1
gen ownrshp_rnd=round(ownrshp, 0.01)
gen max_ownrshp_rnd=round(max_ownrshp, 0.01)
replace likely_mainowner=1 if ownrshp_rnd==max_ownrshp_rnd
drop ownrshp_rnd max_ownrshp_rnd
drop if ownrshp==0
bysort shnro: egen temp = sum(likely_mainowner)
drop if temp==0
drop temp
preserve
collapse likely_mainowner, by(shnro)
tab likely_mainowner
count
count if likely_mainowner<1
count if likely_mainowner==1
restore
keep if likely_mainowner==1
keep shnro vuosi
gen mainowner=1
collapse mainowner, by(shnro vuosi)
save "$data\99_temp\mainowner_dummies.dta", replace

* Re-read the data in
use "$data\01_main\folk_flown_merge.dta", clear
egen long id = group(shnro) // numerical id
order shnro id

* Identify change from wage earner or UIE to IE
merge m:1 shnro vuosi using "$data\99_temp\mainowner_dummies.dta"
drop if _merge==2
drop _merge
erase "$data\99_temp\mainowner_dummies.dta"
xtset id vuosi, yearly // defining as a panel
gen d_ie = D.ie if mainowner==1 // 1 = new owner, -1 = new "non-owner"; we focus on main owners at start
*drop mainowner
gen new_ie=.
replace new_ie=1 if d_ie==1
drop d_ie
order shnro-ie new_ie
replace new_ie=0 if new_ie==.
count if new_ie==1 // 113 384 start from wage earner; 40 448 from UIE
count if new_ie==1 & L.uie==1
count if new_ie==1 & L.uie==0

* Identify change from wage earner or IE to UIE
gen d_uie = D.uie // 1 = new owner, -1 = new "non-owner"
gen new_uie=.
replace new_uie=1 if d_uie==1 // this has repeaters
drop d_uie
order shnro-uie new_uie
replace new_uie=0 if new_uie==.
* To drop the repeaters from the new
bysort id: egen sum_new_uie = total(new_uie)
count if new_uie==1 & sum_new_uie==1 // 295 611 first-timers
count if new_uie==1 & sum_new_uie>1 // 69 510 repeaters
replace new_uie=0 if sum_new_uie>1 // we are not interested in the repeaters
count if new_uie==1 & L.ie==1 // 293 248 start from wage earner, 2 363 from UIE
drop sum_new_uie

order shnro-new_uie
label var new_ie "New incorporated entrepreneur"
label var new_uie "New unincorporated entrepreneur"

save "$data\01_main\folk_flown_merge.dta", replace

/*
________________________________________________________________________________
Merge the industry codes
- wage earners: the firm they worked in
- IEs: the first firm they own
*/

* Matching non-entrepreneurs with firms
use vuosi shnro atv syrtun using "D:\ready-made\FOLK_jaksot_a\tyosuhde_2.dta", clear
keep if atv==1
save "$data\99_temp\tyosuhde_0618.dta", replace
use vuosi shnro atv syrtun using "D:\ready-made\FOLK_jaksot_a\tyosuhde_1.dta", clear
drop if vuosi<1995
keep if atv==1
append using "$data\99_temp\tyosuhde_0618.dta"
erase "$data\99_temp\tyosuhde_0618.dta"
drop atv
rename syrtun syrtun_we
save "$data\99_temp\tyosuhde.dta", replace

* Matching entrepreneurs with firms
use "$data\99_temp\flownyrtti.dta", clear
bysort shnro vuosi: egen max_ownrshp = max(ownrshp) // because of multiple
keep if ownrshp==max_ownrshp
keep syrtun shnro
rename syrtun syrtun_ie
save "$data\99_temp\yritsuhde.dta", replace

* Matching firms with industries
use vuosi syrtun tol08_2 using "D:\ready-made\FIRM_ENTER\yritystol8819.dta", replace
drop if vuosi<1995
save "$data\99_temp\tol.dta", replace

* Matching the three above with the main data
use "$data\01_main\folk_flown_merge.dta", clear
merge 1:1 shnro vuosi using "$data\99_temp\tyosuhde.dta"
drop if _merge==2
drop _merge
replace syrtun_we="" if ie==1 | uie==1
merge m:1 shnro using "$data\99_temp\yritsuhde.dta"
drop if _merge==2
drop _merge
replace syrtun_ie="" if ie!=1
drop ownrshp yrtti
rename syrtun_ie syrtun
replace syrtun=syrtun_we if syrtun==""
drop syrtun_we
merge m:1 syrtun vuosi using "$data\99_temp\tol.dta"
drop if _merge==2
drop _merge
erase "$data\99_temp\tyosuhde.dta"
erase "$data\99_temp\yritsuhde.dta"
erase "$data\99_temp\tol.dta"
erase "$data\99_temp\flownyrtti.dta"

sort shnro vuosi
rename tol08_2 tol
save "$data\01_main\folk_flown_merge.dta", replace

/*
________________________________________________________________________________
Refine the starts by looking at firm births
*/

* Get detailed firm-level info
use "D:\ready-made\FIRM_ENTER\til88yr.dta", clear
forval i=89(1)99{
append using "D:\ready-made\FIRM_ENTER\til`i'yr.dta"
}
gen Vuosi=vuosi
tostring Vuosi, replace
gen alk="19"
replace Vuosi=alk+Vuosi
drop vuosi
ren Vuosi vuosi
forval i=0(1)9{
append using "D:\ready-made\FIRM_ENTER\til0`i'yr.dta"
}
forval i=10(1)12{
append using "D:\ready-made\FIRM_ENTER\til`i'yr.dta"
}
destring vuosi tplkm ika, replace force
forval i=13(1)18{
append using "D:\ready-made\FIRM_ENTER\til`i'yr_yty.dta", force
}
duplicates drop syrtun vuosi, force
drop if syrtun==""
keep syrtun vuosi alo* AlPvm ToimAlPvm lv Liike* oik* OikMuotoVuosi verohOikMuoto
destring oik OikMuotoVuosi verohOikMuoto, replace force
order syrtun vuosi
sort syrtun vuosi
* we want only incorporated firms so use the organizational form to define that
gen oy=1 if oik==31 | verohOikMuoto==12
gen pois=1 if OikMuotoVuosi==31 & verohOikMuoto!=12
bys syrtun: egen drop=min(pois)
replace oy=. if drop==1 & oy==1
drop alomuoto
destring lv, replace force
order syrtun vuosi
gen year=substr(aloaika,-2,2) if length(aloaika)==4
replace year="19"+year if length(aloaika)==4
replace year=substr(aloaika,1,4) if length(aloaika)==6
replace year=substr(aloaika,1,4) if length(aloaika)==8
replace year=substr(AlPvm,1,4) if length(AlPvm)==8

preserve
egen id=group(syrtun)
duplicates drop id year, force
tab year
restore 

destring year, replace
replace year=year+1
replace Liikevaihto=lv if vuosi<2013
duplicates drop syrtun vuosi, force
replace Liikevaihto=0 if Liikevaihto==.
keep if Liikevaihto>0
sort syrtun vuosi
* oy==1 here, so that only incorporated are defined with new firm status
bysort syrtun: gen nr = _n if oy==1
gen new_firm=0
replace new_firm=1 if nr==1
keep if new_firm==1
keep if year>1994
keep syrtun year new_firm
ren year vuosi
ren syrtun syrtun_ownrshp
save "$data\99_temp\ie_syrtun_temp.dta", replace

use "$data\01_main\folk_flown_merge.dta", clear
merge m:1 syrtun_ownrshp vuosi using "$data\99_temp\ie_syrtun_temp.dta"
drop if _merge==2
drop _merge
replace new_firm=0 if new_firm==.
tab vuosi new_firm
label var new_firm "1 if person established an incorporated business that year"
* define individual start for an owner based on the date of firm birth  AND  individual is the MAIN owner in the shareholder panel or flown 
replace new_firm=0 if mainowner==.
tab vuosi new_firm
* make sure that all those who have a change in accumulated retained earnings have IE tags
replace ie=1 if has_d_are==1
erase "$data\99_temp\ie_syrtun_temp.dta"
save "$data\01_main\folk_flown_merge.dta", replace
clear